/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} target
 * @return {number}
 */
var closestValue = function(root, target) {
  let resultNodeVal = null, resultClearanceValue = 2 ** (32);
  function preView(node) {
    if(!node) return ;
    let res = node.val - target;
    res = res < 0 ? -res : res;
    if(res < resultClearanceValue) {
      resultNodeVal = node.val;
      resultClearanceValue = res;
    }
    preView(node.left);
    preView(node.right);
  }
  preView(root);
  return resultNodeVal;
};
